<?php
/**
 * Created by PhpStorm.
 * User: 徐州
 * Date: 2017/5/8
 * Time: 19:26
 */

namespace App\Http\Controllers\Vms;

use App\App\Services\SyncCacheService;
use App\App\Services\VMSService;
use App\Http\Controllers\Controller;
use App\Models\Resource\ResourceService;
use EasyWeChat\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;

class VmsController extends Controller
{
    public function callback(
        Request $request,
        VMSService $VMSService,
        SyncCacheService $syncCacheService,
        ResourceService $resourceService
    )
    {
        if ($request->isJson()) {
            $resource = $request->json()->all();
        } else {
            $raw = $request->getContent();
            $resource = json_decode($raw, true);
        }

        if (empty($resource)) {
            return $VMSService->callbackResponse('', 0, 'data error');
        }

        if (Config::get('app.debug')) {
            Log::debug($resource);
        }

        $resourceID = Arr::get($resource, 'id');

        if (null === $resourceID) {
            return ['code' => 10000, 'message' => '可能不是来自VMS的回调请求'];
        }

        $addressMp4 = Arr::get($resource, 'mp4Address');
        $addressVod = Arr::get($resource, 'vodAddress');
        $addressTs = Arr::get($resource, 'tsAddress');
        $imageList = Arr::get($resource, 'imageList');
        if (count($imageList) > 1) {
            $imageList = $imageList[1];
        } else {
            $imageList = empty($imageList) ? [] : $imageList[0];
        }

        if (!empty($addressVod)) {
            $address = $addressVod;
        } elseif (!empty($addressMp4)) {
            $address = $addressMp4;
        } elseif (!empty($addressTs)) {
            $address = $addressTs;
        } else {
            return $VMSService->callbackResponse('', 0, 'empty file');
        }

        $host = Arr::get($address, 'host');
        $index = array_search('标清', Arr::get($address, 'formats', []));
        $of = sprintf('clips.0.urls.%s', $index);
        $url = $host . Arr::get($address, $of, '');

        $success = $resourceService->create([
            'resource_id'  => $resourceID,
            'resource_url' => $url,
            'type'         => 3,
            'candidate_id' => '',
            'cover_url'    => isset($imageList['imagePath']) ? $imageList['imagePath'] : '',
        ]);

        if ($success) {
            $syncCacheService->updateCacheItem($resourceID);

            return $VMSService->callbackResponse($resourceID, 1, 'ok');
        } else {
            return $VMSService->callbackResponse($resourceID, 0, 'fail');
        }
    }

    /**
     * 上传视频、语音到VMS 上
     *
     * @param Request $request
     * @param         $VMSService
     */
    public function upload(Request $request, VMSService $VMSService)
    {
        $file = $request->file('upfile');
        $VMSService->uploadVideoTmp($file);
        $response = $VMSService->ftpUploadVideo();

        if (is_array($response)) {
            $id = Arr::get($response, '0.id');
            $data = ['code' => 0, 'id' => $id];
        } else {
            $data = ['code' => 504, 'message' => '服务器报错'];
        }

        echo json_encode($data);
    }
}
